package saxion.speelveld.spaceengineer.database;

import saxion.speelveld.spaceengineer.model.Resource;
import saxion.speelveld.spaceengineer.model.ResourceType;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ResourceDAO
{

	private SQLiteDatabase database;

	private ResourceDB dbHelper;

	private static int connectionsOpen = 0;

	public ResourceDAO(Context context)
	{
		dbHelper = new ResourceDB(context);
	}

	/**
	 * Open the database connection.
	 * 
	 * @throws SQLException
	 */
	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
		// connectionsOpen += 1;
		// Log.e("ResourceDAO.open", "open connections: " + connectionsOpen);
	}

	/**
	 * Close the database connection.
	 */
	public void close()
	{
		dbHelper.close();
		// connectionsOpen -= 1;
		// Log.e("ResourceDAO.open", "open connections: " + connectionsOpen);
	}

	public Resource[] getResourcesForGame(int gameId)
	{
		Resource[] resources = new Resource[ResourceType.values().length];

		Cursor cursor = database.rawQuery("SELECT * FROM " + ResourceDB.TABLE_NAME + " WHERE "
			+ ResourceDB.COLUMN_GAMEID + " = " + gameId, null);

		cursor.moveToFirst();

		while (cursor.isAfterLast() == false)
		{
			Resource r = CursorToResource(cursor);
			resources[r.getResourceType().ordinal()] = r;
			cursor.moveToNext();
		}

		cursor.close();
		return resources;
	}

	private Resource CursorToResource(Cursor cursor)
	{
		int iResType = cursor.getInt(cursor.getColumnIndex(ResourceDB.COLUMN_RESOURCEORDINAL));
		int iResAmount = cursor.getInt(cursor.getColumnIndex(ResourceDB.COLUMN_RESOURCEAMOUNT));
		ResourceType resType = ResourceType.values()[iResType];
		Resource resource = new Resource(resType, iResAmount);
		return resource;
	}

	private boolean isThisResourceAlreadyDefinedInThisGame(int gameId, ResourceType resourceType)
	{
		Cursor cursor = database.rawQuery("SELECT * FROM " + ResourceDB.TABLE_NAME + " WHERE " +
			ResourceDB.COLUMN_GAMEID + " = " + gameId + " AND " +
			ResourceDB.COLUMN_RESOURCEORDINAL + " = " + resourceType.ordinal() + "", null);

		cursor.moveToFirst();
		int count = cursor.getCount();
		cursor.close();
		if (count == 1)
		{
			return true;
		}
		return false;
	}

	public void putResourceForGame(int gameId, Resource resource)
	{
		if (isThisResourceAlreadyDefinedInThisGame(gameId, resource.getResourceType()) == true)
		{
			// Log.d("putResourceForGame", "updating " + resource.getResourceType().toString() + ", amount: " +
			// resource.getAmount());
			// update
			String whereClause = ResourceDB.COLUMN_GAMEID + "=" + gameId + " AND " + ResourceDB.COLUMN_RESOURCEORDINAL
				+ "=" + resource.getResourceType().ordinal();
			ContentValues values = new ContentValues();
			values.put(ResourceDB.COLUMN_RESOURCEAMOUNT, resource.getAmount());
			database.update(ResourceDB.TABLE_NAME, values, whereClause, null);
			// Log.d("putResourceForGame", "updated " + updatedRows + " rows");
		}
		else
		{
			// Log.d("putResourceForGame", "inserting new.");
			// insert
			ContentValues values = new ContentValues();
			values.put(ResourceDB.COLUMN_GAMEID, gameId);
			values.put(ResourceDB.COLUMN_RESOURCEAMOUNT, resource.getAmount());
			values.put(ResourceDB.COLUMN_RESOURCEORDINAL, resource.getResourceType().ordinal());
			database.insert(ResourceDB.TABLE_NAME, null, values);
		}
	}

	public void beginTransactionMode()
	{
		database.beginTransaction();
	}

	public void setTransactionModeSuccesfull()
	{
		database.setTransactionSuccessful();
	}

	public void endTransactionMode()
	{
		database.endTransaction();
	}
}
